var express = require('express');
var exphbs  = require('express-handlebars');
var hbs=exphbs.create({
    defaultLayout:'main',
    helpers:{
        section: function (name, opts) {
            if(!this._sections)this._sections={};
            this._sections[name]=opts.fn(this);
            return null;
        },
        addOne: function (idx) {
            return parseInt(idx)+1;
        },
        foo: function () {
            return 'FOO';
        },
        bar: function () {
            return 'BAR';
        },
        fullname: function (person) {
            return person.firstName+' '+person.lastName;
        },
        nav: function (data, opts) {
            console.log('nav data:',data);
            var str='<ul>';
            data.forEach(function (nav) {
                str+='<li>'+opts.fn(nav)+'</li>';
            });
            str+='</ul>';
            return str;
        },
        users: function (data, opts) {
            console.log('users opts:',opts);
            var str='<ul id="'+opts.hash.id+'" class="'+opts.hash.class+'" data-no="'+opts.hash.undefinedNameValue+'">';
            data.forEach(function (user) {
                str+='<li>'+opts.fn(user)+'</li>';
            });
            str+='</ul>';
            return str;
        },
        compare: function (v1, v2,opts) {
            if(v1<v2){
                return opts.fn(this);
            }else{
                return opts.inverse(this);
            }
        },
        isEven: function (val,opts) {
            if(0==val%2){
                return opts.fn(this);
            }else{
                return opts.inverse(this);
            }
        }
    }

});
var port=3007;
var app = express();

app.engine('hbs',hbs.engine);
app.set('view engine', 'hbs');
app.set('port',port);

app.get('/', function (req, res) {
    res.render('home',{title:'Home',foo:'Foo',bar:'Bar'});
});


//expression:表达式
app.get('/expression', function (req, res) {
    res.render('expression',{title:'expression(表达式)',obj:{
        show:true,
        version:'v3.2',
        category:'node',
        'date~':'2016年9月28日'
    }});
});

//Helper
app.get('/helper', function (req, res) {
    res.render('helper',{title:'Helper'});
});

app.get('/fullname', function (req, res) {
    var data={
        author:{
            firstName:'Lei',
            lastName:'Li'
        },
        comments:[
            {firstName:'San',lastName:'Zhang'},
            {firstName:'Si',lastName:'Li'},
            {firstName:'Wu',lastName:'Wang'}
        ]
    };

    res.locals.title='自定义标签：fullname';
    res.render('fullname',data);
});

//块表达式
app.get('/block', function (req, res) {
    res.render('block',{
        title:'块表达式',
        list:[
            {url:'/nav-1',title:'Nav-1'},
            {url:'/nav-2',title:'Nav-2'},
            {url:'/nav-3',title:'Nav-3'}
        ]
    })
});


//each
app.get('/each', function (req, res) {
    res.render('each',{
        title:'each--',
        list:[
            [
                {teacher:'Tom',number:20},
                {teacher:'Lili',number:30},
                {teacher:'Lucy',number:25}
            ],
            [
                {teacher:'Tom2',number:20},
                {teacher:'Lili2',number:30},
                {teacher:'Lucy2',number:25}
            ]
        ],
        list:[1,2,3],
        list1:[],
        list2:[,,],
        list3:[undefined,undefined,undefined],
        list4:['',false,null,0]
    });
});

//each嵌套
app.get('/each2', function (req, res) {
    res.render('each2',{
        title:'each嵌套+HTML转码',
        list:[
            [1,3,4],
            [4,5,6],
            [7,8,9]
        ],
        users:[
            {name:'A',homePage:'<a href="javascript:void 0;">Page A</a>'},
            {name:'B',homePage:'<a href="javascript:void 0;">Page B</a>'},
            {name:'C',homePage:'<a href="javascript:void 0;">Page C</a>'}
        ],
        obj:{
            zero:{
                en:'zero'
            },
            one:{
                en:'one'
            },
            two:{
                en:'two'
            }
        }
    });
});


//each循环中省略this
app.get('/each3', function (req, res) {
    res.render('each3',{
        title:'each循环中省略this',
        users:[
            {name:'A',homePage:'<a href="javascript:void 0;">Page A</a>'},
            {name:'B',homePage:'<a href="javascript:void 0;">Page B</a>'},
            {name:'C',homePage:'<a href="javascript:void 0;">Page C</a>'}
        ]
    });
});


//lookup
app.get('/lookup', function (req, res) {
    res.render('lookup',{
        title:'Lookup',
        groups:[
            {id:11,title:'G1'},
            {id:21,title:'G2'},
            {id:31,title:'G3'}
        ],
        users:[
            {id:1,name:'A'},
            {id:2,name:'B'},
            {id:3,name:'C'}
        ],
        infos:['One','Two','Three']
    });
});


//lookup
app.get('/lookup2', function (req, res) {
    res.render('lookup2',{
        title:'Lookup2',
        users:[
            {id:1,name:'A'},
            {id:2,name:'B'},
            {id:3,name:'C'}
        ]
    });
});



//with
app.get('/with', function (req, res) {
    res.render('with',{
        title:'with',
        users:[
            {id:1,name:'A'},
            {id:2,name:'B'},
            {id:3,name:'C'}
        ]
    });
});


//if
app.get('/if', function (req, res) {
    res.render('if',{
        title:'if',
        falseValues:[
            {val:false,str:'false'},
            {val:!!0,str:'!!0'},
            {val:!!1,str:'!!1'},
            {val:0,str:'0'},
            {val:+0,str:'+0'},
            {val:-0,str:'-0'},
            {val:'',str:'空字符串'},
            {val:null,str:'null'},
            {val:undefined,str:'undefined'},
            {val:[],str:'[]'},
            {val:{},str:'{}'},
            {val:new Boolean(false),str:'new Boolean(false)'},
            {val:NaN,str:'NaN'}
        ],
        show:true,
        list:[
            {name:'A',age:20},
            {id:'B',age:23},
            {name:'C',age:26}
        ],
        users:[
            {name:'D',age:10},
            {name:'E',age:40},
            {name:'F',age:20},
            {name:'G',age:50}
        ]
    });
});

function getWeatherData(cb){
    setTimeout(function(){
        cb([
            {name:'SH',forecastUrl:'SH_URL',weather:'Fine',temp:'54.1 F(12.3 C)'},
            {name:'WH',forecastUrl:'SH_URL',weather:'Fine',temp:'54.1 F(12.3 C)'},
            {name:'HZ',forecastUrl:'SH_URL',weather:'Fine',temp:'54.1 F(12.3 C)'},
            {name:'SZ',forecastUrl:'SH_URL',weather:'Fine',temp:'54.1 F(12.3 C)'}
        ]);
    });
}

//局部视图
app.get('/partial',function(req,res){
    getWeatherData(function(data){
        res.render('partial',{title:'partial',list:data});
    });
});

//路径
app.get('/path',(req,res)=>{
    res.render('path',{
        title:'路径',
        host:'localhost:8080',
        links:[
            {title:'当前页面',path:'path'},
            {title:'each',path:'each'},
            {title:'block',path:'block'}
        ]
    });
});
app.get('/path2',(req,res)=>{
    res.render('path2',{    
        host:'localhost',
        links:[
            {title:'each',path:'each'},
            {title:'block',path:'block'}
        ]
    });
});

//注释
app.get('/comment',(req,res)=>{
    res.render('comment',{
        title:'注释'
    });
});


//@index
app.get('/index', function (req, res) {
    res.render('index',{
        title:'@index',
        users:[
            {id:1,name:'A'},
            {id:2,name:'B'},
            {id:3,name:'C'}
        ]
    });
});


//@first
app.get('/first', function (req, res) {
    res.render('first',{
        title:'@first',
        users:[
            {id:1,name:'A'},
            {id:2,name:'B'},
            {id:3,name:'C'}
        ]
    });
});


app.get('/helper_hash',(req,res)=>{
    res.render('helper_hash',{
        title:'Helper的Hash参数',
        users:[
            {title:'User-A',path:'/user-1'},
            {title:'User-B',path:'/user-2'},
            {title:'User-C',path:'/user-3'}
        ]
    });
});

app.get('/helper_inverse',(req,res)=>{
    res.render('helper_inverse',{
        title:'Helper的options中的inverse',
        num:1
    })
});


app.get('/section',(req,res)=>{
   res.render('section',{
       layout:'section',
       title:'Section'
   })
});

app.get('/if-false',(req,res)=>{
   res.render('if-false',{    
       title:'if-false',
       bool:false,
       string:'',
       emptyArray:[],
       emptyObject:{},
       object:{one:1,two:2},
       NULL:null,
       number:0
   })
});

app.listen(port);
console.info('.hbs port:',app.get('port'),'--',new Date().toString());

